package copyengine.ui.component.button
{
    import copyengine.ui.component.CEUIComponent;
    import copyengine.ui.component.button.animation.ICEButtonAnimation;
    import copyengine.ui.touch.CETouchProxy;

    import starling.display.DisplayObject;
    import starling.events.Touch;

    public class CEButton extends CEUIComponent
    {
        private var mOnClickCallback:Function;
        private var mTargetMc:DisplayObject
        private var mTouchProxy:CETouchProxy;

        protected var mAnimation:ICEButtonAnimation;

        public function CEButton(_targetMc:DisplayObject)
        {
            mTargetMc = _targetMc;
            super(mTargetMc);
            mTouchProxy = new CETouchProxy(mTargetMc, onTouchClick, onTouchDown, onTouchUp, null, null);
        }

        public function set animation(_animation:ICEButtonAnimation):void {mAnimation = _animation;}

        public function set onClickCallBack(_callBack:Function):void {mOnClickCallback = _callBack;}

        private function onTouchUp(_target:DisplayObject, _touch:Touch):void { mAnimation.onTouchUp(_touch); }

        private function onTouchDown(_target:DisplayObject, _touch:Touch):void { mAnimation.onTouchDown(_touch); }

        private function onTouchClick(_target:DisplayObject, _touch:Touch):void
        {
            mAnimation.onTouchUp(_touch);
            doOnClickButton();
            if (mOnClickCallback != null) {
                if (mOnClickCallback.length == 1) {
                    mOnClickCallback(this);
                }
                else {
                    mOnClickCallback();
                }
            }
        }

        protected function doOnClickButton():void {}

    }
}
